import { sp } from "cc"
import { _decorator, Component, Node, Animation } from "cc"
const { ccclass, property } = _decorator

@ccclass("DemoTest")
export class DemoTest extends Component {
  protected start(): void {
    this.playAnimation("roleAttack", false)

    this.scheduleOnce(() => {
      this.playAnimation("roleIdle", true)
    }, 1)

    this.scheduleOnce(() => {
      this.playAnimation("roleWalk", true)
    }, 3)
  }

  /**
   * 播放玩家骨骼动画
   *
   * @param {string} skeletonType 动画类型
   * @param {boolean} [isLoop=false] 是否循环
   * @param {Function} [callback] 回调函数
   * @param {number} [callback] 调用播放动画的位置，方便用于测试
   * @returns
   */
  // @property(sp.Skeleton)
  // public skeleton: sp.Skeleton = null



  // protected skeletonType: string = "" //动画类型

  // public playSkeleton(skeletonType: string, loop: boolean = false, callback?: Function) {
  //   if (!this.skeleton || (this.skeletonType == skeletonType && loop)) {
  //     return
  //   }
  //   this.skeletonType = skeletonType
  //   let animationState = this.skeleton.getState()
  //   animationState.setAnimation(0, skeletonType, loop)
  //   if (!loop) {
  //     this.skeleton.setCompleteListener(() => {
  //       callback && callback()
  //       this.skeleton.setCompleteListener(null)
  //     })
  //   }
  // }


  @property(Animation)
  public animation: Animation = null
  private animationName: string = ""
  protected callbackAnimate: (...args: any[]) => void
  /**
   * 播放玩家帧动画
   */
  public playAnimation(animationName: string, loop: boolean = true, callback?: (...args: any[]) => void) {
    // return
    if (this.animationName == animationName && loop) return
    this.animationName = animationName
    if (this.callbackAnimate) this.callbackAnimate = null
    this.animation.play(animationName)
    if (!loop) {
      if (callback) {
        this.callbackAnimate = callback
        this.animation.once(Animation.EventType.FINISHED, this.callbackAnimate, this)
      }
    }
  }
}
